package com.mariusz.panels;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import java.awt.BorderLayout;
import javax.swing.table.DefaultTableModel;
import javax.swing.border.LineBorder;

import com.mariusz.data.dto.Property;
import com.mariusz.managers.DataManager;
import com.mariusz.managers.NavigationManager;
import com.mariusz.managers.NavigationManager.Screen;
import com.mariusz.managers.PropertyManager;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.ListSelectionModel;
import javax.swing.JLabel;
import java.awt.GridLayout;
import javax.swing.border.BevelBorder;

import org.jdesktop.swingx.JXTable;
import org.jdesktop.swingx.JXTitledPanel;

import java.awt.Font;
import org.jdesktop.swingx.JXHeader;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import javax.swing.JScrollPane;
import org.jdesktop.swingx.JXImageView;
import java.awt.Toolkit;
import org.jdesktop.swingx.ScrollableSizeHint;
import org.jdesktop.swingx.JXButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class PropertiesPanel extends JXTitledPanel implements ActionListener {

	private JXTable tblProperties;
	private PropertyManager manager = PropertyManager.getInstance();
	private JXButton btnEditProperty;
	private JXButton btnAddProperty;
	private JXButton btnBack;
	private JXButton btnRemoveProperty; 
	private JLabel lblPropertiesTableError;
	private JXButton btnLocators;
	
	public PropertiesPanel() {
		getContentContainer().setBackground(Color.WHITE);
		setTitleForeground(new Color(0, 0, 0));
		setTitle("Renting properties list:");
		getContentContainer().setBounds(10, 10, 100, 100);
		getContentContainer().setLayout(null);

		JScrollPane propertiesScrollPane = new JScrollPane();
		getContentContainer().add(propertiesScrollPane);
		propertiesScrollPane.setBounds(10, 232, 664, 191);
		tblProperties = new JXTable(manager.getTableContent());
		tblProperties.setSortsOnUpdates(false);
		tblProperties.setSortable(false);
		tblProperties.setColumnControlVisible(true);
		propertiesScrollPane.setViewportView(tblProperties);

		btnEditProperty = new JXButton();
		btnEditProperty.addActionListener(this);
		btnEditProperty.setText("Edit Property");
		btnEditProperty.setBounds(550, 115, 124, 28);
		getContentContainer().add(btnEditProperty);

		btnAddProperty = new JXButton();
		btnAddProperty.addActionListener(this);
		btnAddProperty.setText("Add Property");
		btnAddProperty.setBounds(415, 115, 125, 28);
		getContentContainer().add(btnAddProperty);

		btnBack = new JXButton();
		btnBack.addActionListener(this);
		btnBack.setText("Back");
		btnBack.setBounds(549, 154, 125, 28);
		getContentContainer().add(btnBack);

		lblPropertiesTableError = new JLabel("");
		lblPropertiesTableError.setForeground(Color.RED);
		lblPropertiesTableError.setBounds(10, 213, 369, 14);
		getContentContainer().add(lblPropertiesTableError);
		
		btnRemoveProperty = new JXButton();
		btnRemoveProperty.addActionListener(this);
		btnRemoveProperty.setText("Remove Property");
		btnRemoveProperty.setBounds(415, 154, 125, 28);
		getContentContainer().add(btnRemoveProperty);
		
		ResourceImagePanel imagePanel = new ResourceImagePanel("Stokesay-Castle.jpg");
		getContentContainer().add(imagePanel);
		imagePanel.setBounds(10, 11, 395, 191);
		
		btnLocators = new JXButton();
		btnLocators.setText("Tenants");
		btnLocators.setBounds(415, 36, 259, 54);
		getContentContainer().add(btnLocators);
		btnLocators.addActionListener(this);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == btnAddProperty) {
			Property property = new Property();
			DataManager.getInstance().setCurrentProperty(property);
			NavigationManager.getInstance().switchTo(Screen.PROPERTY_EDIT);

		} else if (e.getSource() == btnEditProperty) {
			if (validateSelection()) {
				int row = tblProperties.getSelectedRow();
				Property property = DataManager.getInstance().getProperties()
						.get(row);
				DataManager.getInstance().setCurrentProperty(property);
				NavigationManager.getInstance().switchTo(Screen.PROPERTY_EDIT);
			}
		} else if (e.getSource() == btnRemoveProperty){
			if (validateSelection()) {
				
				int row = tblProperties.getSelectedRow();
				Property property = DataManager.getInstance().getProperties()
						.get(row);
				int answer = JOptionPane.showConfirmDialog(this, "Are you really sure you want to delate the "+property.getName()+"?");
			    if (answer == JOptionPane.YES_OPTION) {
			    	DataManager.getInstance().getProperties().remove(property);
					this.show(true);
			    } else if (answer == JOptionPane.NO_OPTION) {
			      // User clicked NO.
			    }
				
			}
		} else if (e.getSource() == btnBack) {
			NavigationManager.getInstance().switchTo(Screen.STATUS_PANEL);
		}else if(e.getSource() == btnLocators){
			if (validateSelection()) {
				int row = tblProperties.getSelectedRow();
				Property property = DataManager.getInstance().getProperties()
						.get(row);
				DataManager.getInstance().setCurrentProperty(property);
				NavigationManager.getInstance().switchTo(Screen.LOCATORS);
			}
		}
	}

	public boolean validateSelection() {
		if (tblProperties.getSelectedRow() >= 0) {
			lblPropertiesTableError.setText("");
			return true;
		} else {
			lblPropertiesTableError.setText("Please select the Property");
			return false;
		}
	}

	@Override
	public void show(boolean b) {
		this.tblProperties.setModel(manager.getTableContent());
		super.show(b);

	}
}
